/*
 *
 * Copyright (c) 2017 Ericsson AB Corporation, all rights reserved.
 *
 */

package com.roy.spring.multitenant.controller;

import java.util.Date;

import javax.websocket.server.PathParam;

import com.roy.spring.multitenant.model.User;
import com.roy.spring.multitenant.model.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.roy.spring.multitenant.model.Order;
import com.roy.spring.multitenant.model.OrderRepository;
import com.roy.spring.multitenant.tenant.TenantContext;

/**
 *
 *
 * @author elloxxn
 * 
 */
@Controller
public class OrderController {

    @Autowired
    private OrderRepository orderRepository;

    @Autowired
    private UserRepository userRepository;

    @RequestMapping(path = "/orders", method= RequestMethod.POST)
    public ResponseEntity<?> createSampleOrder(@RequestHeader("X-TenantID") String tenantName) {
        TenantContext.setTenant(tenantName);

        Order newOrder = new Order();
        orderRepository.save(newOrder);

        return ResponseEntity.ok(newOrder);
    }
    @RequestMapping(path = "/orders/{id}", method= RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<Order> createSampleOrder(@RequestHeader("X-TenantID") String tenantName, @PathVariable("id") Long id) {
        //TenantContext.setTenant(tenantName + ",common");

        Order order = orderRepository.findOne(id);

        User user = userRepository.findByUsername("test");

        System.out.println(user + "=======================");
        return ResponseEntity.ok(order);
    }
}
